home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / SNIP0492.ARJ / EDITGETS.C < prev    next >
C/C++ Source or Header  |  1991-09-23  |  2KB  |  65 lines

  1. /*
  2.  *  Get a string with editing functions by Paul Roub
  3.  *
  4.  *  This is somewhat off the top of my head, but it should work.  It
  5.  *  assumes that backspace is not destructive on your display
  6.  *  (although we'll MAKE it destructive when editing), and that
  7.  *  Control-G sounds a bell (which will be the 'message' you asked
  8.  *  for).  Input is terminated by a CR.  It also assumes that you
  9.  *  have the function getch() (or a work-alike, which gets a
  10.  *  character from the keyboard WITHOUT echo.
  11.  *
  12.  */
  13.  
  14. #include  <conio.h>                       /* MSC needs this for getch() */
  15. #include  <ctype.h>
  16. #include  <stdio.h>
  17.  
  18.  
  19. #define   BELL      7                     /* Ctrl-G                     */
  20. #define   BS        8                     /* backspace (Ctrl-H)         */
  21. #define   CR       13                     /* Carriage return            */
  22.  
  23. void GetStr(char *st, int maxlen)
  24. {
  25.       int len = 0, ch;
  26.  
  27.       putchar('[');                       /* display our 'field'        */
  28.       for (len = 0; len < maxlen; len++)
  29.             putchar(' ');
  30.       putchar(']');
  31.  
  32.       putchar(BS);                        /* and get into position      */
  33.       for (len = maxlen; len > 0; len--)
  34.             putchar(BS);
  35.  
  36.       len = 0;
  37.  
  38.       while ((ch = getch()) != CR)
  39.       {
  40.             if (ch == BS)                 /* backspace                  */
  41.             {
  42.                   if (len == 0)           /* already at beginning?      */
  43.                         putchar(BELL);    /*   yes, complain            */
  44.                   else
  45.                   {                       /*   no, backup and space     */
  46.                         len--;
  47.                         st--;
  48.                         putchar(BS);
  49.                         putchar(' ');
  50.                         putchar(BS);
  51.                   }
  52.             }
  53.             else if (iscntrl(ch) || (len == maxlen))  /* illegal or     */
  54.                   putchar(BELL);                      /* too long       */
  55.             else
  56.             {                             /* useable character          */
  57.                   *st++ = (char)ch;       /* put it in string           */
  58.                   len++;                  /* note another character     */
  59.                   putchar(ch);            /* and display                */
  60.             }
  61.       }
  62.       *st = 0;                            /* terminate the string       */
  63.       return;
  64. }
  65.